//! 获取当前上下文 task

use task_define::Task;

/// 当前 task 引用
#[inline(always)]
pub fn current() -> &'static Task {
    cfg_if::cfg_if! {
        if #[cfg(target_arch = "aarch64")] {
            unsafe {
                let task: usize;
                core::arch::asm!(
                    "mrs {0}, sp_el0",
                    out(reg) task
                );
                &*(task as *const Task)
            }
        } else {
            unreachable!()
        }
    }
}

/// 当前 task 可变引用
#[inline(always)]
pub fn current_mut() -> &'static mut Task {
    cfg_if::cfg_if! {
        if #[cfg(target_arch = "aarch64")] {
            unsafe {
                let task: usize;
                core::arch::asm!(
                    "mrs {0}, sp_el0",
                    out(reg) task
                );
                &mut *(task as *mut Task)
            }
        } else {
            unreachable!()
        }
    }
}

/// 记录当前 task 地址
///
/// ### 注意:
/// 记录 task 的原始指针, 因此不能直接使用, 只能由汇编代码及任务切换代码使用
#[no_mangle]
pub static PERCPU_ENTRY_TASK: [usize; 8] = [0; 8];
